package com.manager.devicemanager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.config.JwtUtil;
import com.manager.devicemanager.entity.DeviceUser;
import com.manager.devicemanager.entity.OperationLog;
import com.manager.devicemanager.entity.UserInfo;
import com.manager.devicemanager.service.IDeviceInfoService;
import com.manager.devicemanager.service.IDeviceUserService;
import com.manager.devicemanager.service.IUserInfoService;
import com.manager.devicemanager.service.OperationLogService;
import com.manager.devicemanager.vo.OperationLogVO;
import com.manager.devicemanager.vo.PageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-05-17
 */
@Tag(name = "日志", description = " 日志相关接口")
@RestController
@RequestMapping("/operationLog")
public class OperationLogController {

    @Autowired
    private OperationLogService operationLogService;
    @Autowired
    private IDeviceInfoService deviceInfoService;
    @Autowired
    private HttpServletRequest httpServletRequest;
   @Autowired
   private IUserInfoService userInfoService;
   @Autowired
   private IDeviceUserService iDeviceUserService;

    @Operation(summary = "日志分页查询")
    @PostMapping("/page")
    public Result<IPage<OperationLogVO>> page(@Parameter(description = "分页查询条件对象") @RequestBody PageVO<OperationLogVO> pageVO) {
        try {
            // 获取分页参数
            int pageNum = pageVO.getPageNum();
            int pageSize = pageVO.getPageSize();
            // 获取查询条件
            OperationLogVO condition = pageVO.getCondition();
            if (condition==null){
                condition=new OperationLogVO();
            }
            String tokenFromRequest = JwtUtil.getTokenFromRequest(httpServletRequest);
            String userid = JwtUtil.extractUserId(tokenFromRequest);
//            UserInfo userInfo = userInfoService.getById(userid);
            if (!"1".equals(userid)){
                condition.setUserid(userid);
                List<DeviceUser> deviceUserInfoByUserid = iDeviceUserService.getDeviceUserInfoByUserid(userid);
                condition.setDeviceids(deviceUserInfoByUserid.stream().map(DeviceUser::getDeviceid).collect(Collectors.toList()));
            }
            // 创建分页对象并执行查询
            Page<OperationLogVO> page = new Page<>(pageNum, pageSize);
            IPage<OperationLogVO> pageResult = operationLogService.getPage(page, condition);

            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败"+e.getMessage());
        }

    }


    @Operation(summary = "设备操作记录")
    @PostMapping("/devicepage")
    public Result<IPage<OperationLogVO>> devicepage(@Parameter(description = "分页查询条件对象") @RequestBody PageVO<OperationLogVO> pageVO) {
        try {
            // 获取分页参数
            int pageNum = pageVO.getPageNum();
            int pageSize = pageVO.getPageSize();
            // 获取查询条件
            OperationLogVO condition = pageVO.getCondition();
            if (condition==null){
                condition=new OperationLogVO();
            }
            // 创建分页对象并执行查询
            Page<OperationLogVO> page = new Page<>(pageNum, pageSize);
            IPage<OperationLogVO> pageResult = operationLogService.getPage(page, condition);

            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败"+e.getMessage());
        }

    }


}
